package cn.tedu.zblogs.home.service.impl;

import cn.tedu.zblogs.home.mapper.ArticleMapper;
import cn.tedu.zblogs.home.mapper.ArticleTagMapper;
import cn.tedu.zblogs.home.mapper.TagMapper;
import cn.tedu.zblogs.home.pojo.VO.ArticleEsVO;
import cn.tedu.zblogs.home.pojo.VO.ArticleTagVO;
import cn.tedu.zblogs.home.pojo.VO.ArticleVO;
import cn.tedu.zblogs.home.pojo.VO.TagVO;
import cn.tedu.zblogs.home.service.EsService;
import cn.tedu.zblogs.home.utils.EsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class EsServiceImpl implements EsService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private EsUtil esUtil;
    @Autowired
    private ArticleTagMapper articleTagMapper;


    /**
     * 将文章数据插入es数据库   在文章插入和更新的时候调用
     * @param id
     * @return
     */
    @Override
    public boolean addArticleEsData(Integer id) {
        //查询文章数据
        ArticleEsVO articleEsVO = articleMapper.selectArticleOne(id);
        log.info("articleEsVO:{}",articleEsVO);
        System.out.println(articleEsVO);

        //增加es数据
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("id", articleEsVO.getId());
        jsonMap.put("user_id",  articleEsVO.getUserId());
        jsonMap.put("title", articleEsVO.getTitle());
        jsonMap.put("short_title", articleEsVO.getShortTitle());
        jsonMap.put("content", articleEsVO.getContent());


        //es工具类 调用插入数据方法
        boolean b = esUtil.addData("article", Long.toString(articleEsVO.getId()), jsonMap);
        return b;
    }


    /**
     * 查询接口
     * @param page
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public List<ArticleVO> selectArticleEsList(Integer page, Integer pageSize, String queryString) {
        //查询es数据
        List<ArticleEsVO> list = esUtil.MatchAllEsData("article", page, pageSize, queryString);

        List<ArticleVO>  returnList= new ArrayList<>();

        for (int i=0;i<list.size();i++){
            ArticleEsVO articleEsVO=list.get(i);
            //取出文章id
            Long articleVOId= articleEsVO.getId();
            //文章id 拿最新的内容(// 根据文章ID查询文章详细信息)
            ArticleVO articleVO= articleMapper.selectArticledetail(articleVOId.intValue());
            // 更新查询到的文章对象的标题
            articleVO.setTitle(articleEsVO.getTitle());
            // 更新查询到的文章对象的内容
            articleVO.setContent(articleEsVO.getContent());


            //文章id 去查询关系表，拿到标签id
            List<ArticleTagVO> list1 = articleTagMapper.ArticleIdSelectTagId(articleVOId);
            if (!list1.isEmpty()){
                System.out.println(list1);
                ArticleTagVO  articleTagVO= list1.get(0);
                int tagId =  articleTagVO.getTagId();
                //标签id 去查询标签表，拿到标签名

                List<TagVO> list2= tagMapper.selectTagName(tagId);
                if (!list2.isEmpty()){//判断list是否为空
                    String tagName= list2.get(0).getTagName();
                    //标签名 赋值给文章对象
                    articleVO.setTagName(tagName);
                }
            }
            returnList.add(articleVO);
        }

        return returnList;
    }
}
